﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Draco.Commons.Container
{
    public class DictionaryIgnoreCase<TKey, TValue> : DictionaryEx<TKey, TValue>
    {
        /// <summary>
        /// 构造
        /// </summary>
        public DictionaryIgnoreCase():base(new IgnoreCaseComparer<TKey>()){}
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dictionary"></param>
        public DictionaryIgnoreCase(IDictionary<TKey, TValue> dictionary)
            : base(dictionary, new IgnoreCaseComparer<TKey>()) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="comparer"></param>
        public DictionaryIgnoreCase(IEqualityComparer<TKey> comparer) : base(comparer) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="capacity"></param>
        public DictionaryIgnoreCase(int capacity) 
            : base(capacity, new IgnoreCaseComparer<TKey>()) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dictionary"></param>
        /// <param name="comparer"></param>
        public DictionaryIgnoreCase(IDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer)
            :base(dictionary,comparer){}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="capacity"></param>
        /// <param name="comparer"></param>
        public DictionaryIgnoreCase(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer) { }
    }

    class IgnoreCaseComparer<TKey> : IEqualityComparer<TKey>
    {
        public bool Equals(TKey x, TKey y)
        {
            if (x is String)
            {
                return String.Compare(x as string, y as string,true)==0;
            }
            return x.Equals(y);
        }

        public int GetHashCode(TKey obj)
        {
            if (obj == null)
                return 0;
            if (obj is String)
                return (obj as String).ToLower().GetHashCode();
            return obj.GetHashCode();
        }
    }
}
